package com.peoit.honeycomb.dto.security;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.peoit.honeycomb.domain.security.Permission;
import com.peoit.honeycomb.dto.BaseDto;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 权限dto
 * Created by zhouqiu on 2015/10/15.
 */
public class PermissionDto extends BaseDto {

    //父权限id
    private String parentPermissionId;

     //父权限name
    private String parentPermissionName;

    //权限所在菜单
    private String inWhere;

    //权限所处级别
    private String level;

    //权限名称
    private String name;
    //描述
    private String description;

    public String getParentPermissionId() {
        return parentPermissionId;
    }

    public void setParentPermissionId(String parentPermissionId) {
        this.parentPermissionId = parentPermissionId;
    }

    public String getParentPermissionName() {
        return parentPermissionName;
    }

    public void setParentPermissionName(String parentPermissionName) {
        this.parentPermissionName = parentPermissionName;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getInWhere() {
        return inWhere;
    }

    public void setInWhere(String inWhere) {
        this.inWhere = inWhere;
    }

    public PermissionDto toDto(Permission permission) {
        PermissionDto dto = new PermissionDto();
        dto.setId(permission.getId());
        dto.setName(permission.getName());
        dto.setDescription(permission.getDescription());
        dto.setLevel(permission.getLevel());
        dto.setParentPermissionId(permission.getParentPermission().getId());
        dto.setParentPermissionName(permission.getParentPermission().getName());
        dto.setInWhere(permission.getInWhere());
        return dto;
    }

    /**
     *
     * @param dto
     * @param permissionParent 父权限
     * @return
     */
    public Permission toEntity(PermissionDto dto,Permission permissionParent) {
        Permission permission = new Permission();
        permission.setId(dto.getId());
        if(StringUtils.isNotBlank(dto.getName()))permission.setName(dto.getName());
        if(StringUtils.isNotBlank(dto.getName()))permission.setDescription(dto.getDescription());
        if(StringUtils.isNotBlank(dto.getName()))permission.setLevel(dto.getLevel());
        if(StringUtils.isNotBlank(dto.getInWhere()))permission.setInWhere(dto.getInWhere());
        if(null != permissionParent){
            permission.setParentPermission(permissionParent);
        }
        return permission;
    }

    public List<PermissionDto> toPageDto(List<Permission> list){
        Collection collection = Collections2.transform(list, new Function<Permission, PermissionDto>() {
            @Override
            public PermissionDto apply(Permission permission) {
                return toDto(permission);
            }
        });

        ArrayList<PermissionDto> plist = Lists.newArrayList(collection);
        return plist;
    }
}
